﻿using System.Collections.Generic;
using Game.Kernel;
using Game.Logic.Manager;
using Common.Utils;
using Sproto;

namespace Game.Logic.Data
{
    public class GameServerData : GameBaseData
    {
        public class ServerInfo
        {
            public string Name { set; get; }
            public string Host { set; get; }
            public long Port { set; get; }
        }

        public GameServerData()
        {
            GameDataManager.Instance.RegisterServerDataListener("handshake", OnReceivedHandshake);
            GameDataManager.Instance.RegisterServerDataListener("auth", OnReceivedAuthData);
            GameDataManager.Instance.RegisterServerDataListener("register", OnReceivedAuthData);

            Reset();
        }

        private byte[] m_clientkey;
        public byte[] ClientKey 
        {
            private set
            {
                m_clientkey = value;
            }
            get
            {
                if (null == m_clientkey)
                    m_clientkey = GameTools.GenerateClientKey();
                return m_clientkey;
            }
        }
        public byte[] ServerKey { private set; get; }
        public byte[] Challenge { private set; get; }
        public byte[] Secret { private set; get; }
        public byte[] HMac { private set; get; }
        public string Token { private set; get; }
        public ServerInfo CdnUrl { private set; get; }
        public List<ServerInfo> ServerList { private set; get; }

        // 重置数据值
        private void Reset()
        {
            CdnUrl = new ServerInfo();
            ServerList = new List<ServerInfo>();
        }

        // 收到握手消息
        private void OnReceivedHandshake(SprotoObject _handshake)
        {
            // UnityEngine.Debug.Log(_handshake.ToString());
            Challenge = GameTools.Base64DecodeToBytes(_handshake["challenge"]);
            ServerKey = GameTools.Base64DecodeToBytes(_handshake["server_key"]);
            Secret = GameTools.DHSecret(ServerKey, ClientKey);
            HMac = GameTools.HMac64(Challenge, Secret);
        }


        // 收到认证消息
        private void OnReceivedAuthData(SprotoObject _auth)
        {
            // UnityEngine.Debug.Log(_auth.ToString());
            Token = _auth["token"];

            ServerList.Clear();
            List<SprotoObject> list = _auth["server_list"];
            foreach (SprotoObject sobj in list)
            {
                ServerInfo serverInfo = new ServerInfo();
                serverInfo.Name = sobj["name"];
                serverInfo.Host = sobj["host"];
                serverInfo.Port = sobj["port"];
                ServerList.Add(serverInfo);
            }

            SprotoObject cdnObj = _auth["cdn"];
            CdnUrl.Name = cdnObj["name"];
            CdnUrl.Host = cdnObj["host"];
            CdnUrl.Port = cdnObj["port"];
        }

        // 获得最优Server
        public ServerInfo GetServer()
        {
            // TODO:筛选延迟最低的server
            return ServerList[0];
        }
    }
}


